package cn.edu.cqupt.cxcy.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cn.edu.cqupt.cxcy.dto.TeamImpl;
import cn.edu.cqupt.cxcy.model.Member;
import cn.edu.cqupt.cxcy.model.Project;
import cn.edu.cqupt.cxcy.util.Tools;

/**
 * Servlet implementation class TeamAddMember
 */
@WebServlet("/TeamAddMember")
public class TeamAddMember extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("text/html;charset=utf-8");
		HttpSession session = request.getSession();
	    List<Object> list = (List<Object>)session.getAttribute("user");
	    int te_id = (int) list.get(1);
	    Member member = new Member();
		DiskFileItemFactory factory = new DiskFileItemFactory();
		String savePath = request.getServletContext().getRealPath("/memberImg");
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("utf-8");
		File f = new File(savePath);
		if(!f.exists()){
			f.createNewFile();
		}
		factory.setRepository(f);
		factory.setSizeThreshold(1024*1024*1024);
		try {
			List<FileItem> items = upload.parseRequest(request);
			for(FileItem item:items){
				if(!item.isFormField() && item != null && !item.getName().equals("") && item.getName() != null){
					//String[] names = new String[3];//字符串数组,分别为文件类型,数据库中存储的文件名,文件保存到服务器的地址
					String logoPath;
					String foot = item.getName().substring(item.getName().indexOf("."));
					String realFileName = Tools.getRandom(10)+foot;
					//String dbName = item.getName().substring(item.getName().lastIndexOf("\\")+1);
					//names[0] = item.getFieldName();//得到文件的名称,可以用这个参数来识别上传的文件类型
					logoPath = "memberImg/"+realFileName;
					//names[2] = dbName;
					File dir = new File(savePath);
					File file = null;
					if(!dir.exists()){
						dir.mkdirs();
					}
					file = new File(savePath,realFileName);
					item.write(file);
					member.setM_logo(logoPath);
				}else if(item.isFormField() && item != null){
					if(item.getFieldName().equals("member_name")){
						member.setM_name(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_sex")){
						member.setM_sex(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_number")){
						member.setM_no(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_telephone")){
						member.setM_tel(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_email")){
						member.setM_email(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_school")){
						member.setM_school(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_college")){
						member.setM_college(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_explain")){
						member.setM_explain(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_type")){
						member.setM_type(item.getString("utf-8"));
					}else if(item.getFieldName().equals("member_id")){
						member.setM_id(Integer.valueOf(item.getString("utf-8")));
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}catch(Exception e1){
			e1.printStackTrace();
		}
		TeamImpl teamImpl = new TeamImpl();
		teamImpl.addMemberInfo(member, te_id);
		
		String pjax_back = "$(function(){$(\"a.turn_pjax[href='team/member']\").trigger('click');});";
		request.setAttribute("pjax_back", pjax_back);
		request.getRequestDispatcher("Tteam.jsp").forward(request, response);
	}

}
